import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.TextAnchor;
import org.jfree.util.ShapeUtilities;

/**
 * 
 * A simple demonstration application showing how to create a line chart using
 * 
 * data from an {@link XYDataset}.
 * 
 *
 */

public class BruceWayneChart extends ApplicationFrame {

	/**
	 * 
	 * Creates a new demo.
	 * 
	 *
	 * 
	 * @param title
	 * 
	 *            the frame title.
	 */

	public BruceWayneChart(final String title) {

		super(title);

		final XYDataset dataset = createDataset();

		final JFreeChart chart = createChart(dataset);

		final ChartPanel chartPanel = new ChartPanel(chart);

		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

		setContentPane(chartPanel);

	}

	/**
	 * 
	 * Creates a sample dataset.
	 * 
	 *
	 * 
	 * @return a sample dataset.
	 */

	private XYDataset createDataset() {

		final XYSeries series1 = new XYSeries(

		" Accumulated minimum guaranteed benefit");
		series1.add(1, 20000);
		series1.add(2, 40600);
		series1.add(3, 61818);
		series1.add(4, 83673);
		series1.add(5, 106183);
		series1.add(6, 129368);
		series1.add(7, 153249);
		series1.add(8, 177847);
		series1.add(9, 203182);

		final XYSeries series2 = new XYSeries(" Accumulated estimated benefit");

		series2.add(1, 20000);
		series2.add(2, 40600);
		series2.add(3, 61818);
		series2.add(4, 83673);
		series2.add(5, 106183);
		series2.add(6, 129368);
		series2.add(7, 168249);
		series2.add(8, 208297);
		series2.add(9, 249546);

		final XYSeries series3 = new XYSeries("Accumulated maximum benefit");
		series3.add(1, 20000);
		series3.add(2, 40600);
		series3.add(3, 61818);
		series3.add(4, 83673);
		series3.add(5, 106183);
		series3.add(6, 129368);
		series3.add(7, 171249);
		series3.add(8, 214387);
		series3.add(9, 258818);

		final XYSeriesCollection dataset = new XYSeriesCollection();

		dataset.addSeries(series1);
		dataset.addSeries(series2);
		dataset.addSeries(series3);

		return dataset;

	}

	private XYDataset createDataset1() {
		final XYSeries series1 = new XYSeries(
				" Accumulated minimum guaranteed benefit");
		series1.add(10, 2029278);
		//series1.add(11, 2029278+100);

		final XYSeries series2 = new XYSeries(" Accumulated estimated benefit");
		series2.add(10, 2242032);
		//series2.add(11, 2242032+100);

		final XYSeries series3 = new XYSeries("Accumulated maximum benefit");
		series3.add(10, 2284583);
		//series3.add(12, 2284583+100);

		final XYSeriesCollection dataset = new XYSeriesCollection();

		dataset.addSeries(series1);
		dataset.addSeries(series2);
		dataset.addSeries(series3);

		return dataset;

	}

	/**
	 * 
	 * Creates a chart.
	 * 
	 *
	 * 
	 * @param dataset
	 * 
	 *            the data for the chart.
	 * 
	 *
	 * 
	 * @return a chart.
	 */

	private JFreeChart createChart(final XYDataset dataset) {

		// create the chart...

		final JFreeChart chart = ChartFactory.createXYLineChart(

		"Accumulated benifit left on deposit", // chart title

				"X", // x axis label

				"Y", // y axis label

				dataset, // data

				PlotOrientation.VERTICAL, true, // include legend

				true, // tooltips

				false // urls

				);

		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

		chart.setBackgroundPaint(Color.white);

		/*
		 * 
		 * final StandardLegend legend = (StandardLegend) chart.getLegend();
		 * 
		 * legend.setDisplaySeriesShapes(true);
		 */

		// get a reference to the plot for further customisation...

		final XYPlot plot = chart.getXYPlot();

		plot.setBackgroundPaint(Color.white);

		// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));

		plot.setDomainGridlinePaint(Color.lightGray);

		plot.setRangeGridlinePaint(Color.lightGray);

		plot.getRangeAxis().setVisible(false);

		final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

		renderer.setSeriesLinesVisible(0, true);

		renderer.setSeriesLinesVisible(1, true);

		renderer.setSeriesLinesVisible(2, true);

		renderer.setSeriesShapesVisible(0, false);

		renderer.setSeriesShapesVisible(1, false);

		renderer.setSeriesShapesVisible(2, false);

		renderer.setBaseItemLabelsVisible(true);

		renderer.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot

		.getLegendItems()));

		plot.setRenderer(renderer);

		// change the auto tick unit selection to integer units only...

		// final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

		// rangeAxis.setTickUnit(new NumberTickUnit(1000));

		// rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		// OPTIONAL CUSTOMISATION COMPLETED.

		return chart;

	}

	// ****************************************************************************

	// * JFREECHART DEVELOPER GUIDE *

	// * The JFreeChart Developer Guide, written by David Gilbert, is available

	// *

	// * to purchase from Object Refinery Limited: *

	// * *

	// * http://www.object-refinery.com/jfreechart/guide.html *

	// * *

	// * Sales are used to provide funding for the JFreeChart project - please *

	// * support us so that we can continue developing free software. *

	// ****************************************************************************

	/**
	 * 
	 * Starting point for the demonstration application.
	 * 
	 *
	 * 
	 * @param args
	 * 
	 *            ignored.
	 */

	/*
	 * 
	 * public static void main(final String[] args) {
	 * 
	 * 
	 * 
	 * final LineChartDemo6 demo = new
	 * 
	 * LineChartDemo6("Accumulated benifit left on deposit"); demo.pack();
	 * 
	 * RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true);
	 * 
	 * 
	 * 
	 * }
	 */

	private void createFrame() {

		NumberAxis xAxis = new NumberAxis("End Of Policy Year");

		xAxis.setRange(Double.parseDouble(".5"), Double.parseDouble("11"));

		xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		xAxis.setTickLabelFont(new Font("Arial", Font.BOLD, 11));

		// LogAxis yAxis = new LogAxis("Value");

		NumberAxis yAxis = new NumberAxis("Value");

		yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		yAxis.setTickUnit(new NumberTickUnit(25000));

		yAxis.setTickLabelsVisible(true);

		//yAxis.setNumberFormatOverride(new DecimalFormat("00"));
		yAxis.setNumberFormatOverride(((DecimalFormat) NumberFormat.getInstance(Locale.US)));
		

		yAxis.setTickLabelFont(new Font("Arial", Font.BOLD, 11));
		//yAxis.setLabelPaint(new java.awt.Color(51, 255, 0));

		//yAxis.setTickLabelPaint(new java.awt.Color(51, 255, 0));

		yAxis.setRange(0, 275000);
		//yAxis.setRange(0, 2500000);
		

		// ---------------------------------------------------------------------------------------
		NumberAxis yAxis1 = new NumberAxis("Value-Secondary");

		yAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		yAxis1.setTickUnit(new NumberTickUnit(250000));

		yAxis1.setTickLabelsVisible(true);

		yAxis1.setNumberFormatOverride(((DecimalFormat) NumberFormat.getInstance(Locale.US)));

		yAxis1.setTickLabelFont(new Font("Arial", Font.BOLD, 11));

		//yAxis1.setRange(0, 275000);
		yAxis1.setRange(25000, 2400000);

		// ---------------------------------------------------------------------------------------
		final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		
		Shape shape  = new Ellipse2D.Double(0,0,5,5);
		renderer.setBaseShape(shape);
		renderer.setBasePaint(Color.red);
		
		//renderer.setSeriesShape(0, ShapeUtilities.createDownTriangle(1));
		//renderer.setSeriesShape(1, ShapeUtilities.createDiamond(1));
		//renderer.setSeriesShape(2, ShapeUtilities.createDiagonalCross(3, 1));

		renderer.setSeriesLinesVisible(0, true);

		renderer.setSeriesLinesVisible(1, true);

		renderer.setSeriesLinesVisible(2, true);

		renderer.setSeriesShapesVisible(0, false);

		renderer.setSeriesShapesVisible(1, false);

		renderer.setSeriesShapesVisible(2, false);

		renderer.setSeriesPaint(0, new java.awt.Color(18, 193, 118));

		renderer.setSeriesPaint(1, new java.awt.Color(204, 51, 51));

		renderer.setSeriesPaint(2, new java.awt.Color(51, 102, 255));

		renderer.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition(

		ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_LEFT));

		renderer.setSeriesPositiveItemLabelPosition(1, new ItemLabelPosition(

		ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER));

		renderer.setSeriesPositiveItemLabelPosition(2, new ItemLabelPosition(

		ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_RIGHT));

		// XYItemLabelGenerator itemLabelGenerator = new

		// StandardXYItemLabelGenerator(

		// "{2}", new DecimalFormat("00"), new DecimalFormat("00"));

		renderer.setItemLabelFont(new Font("Arial", Font.BOLD, 11));

		renderer.setBaseItemLabelsVisible(true);

		XYPlot plot = new XYPlot(createDataset(), xAxis, yAxis, renderer);

		renderer.setBaseItemLabelsVisible(true);

		renderer.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot

		.getLegendItems()));

		plot.getRangeAxis().setVisible(true);

		plot.setOrientation(PlotOrientation.VERTICAL);

		plot.setDomainGridlinePaint(Color.white);

		plot.setRangeGridlinePaint(Color.white);
		// ----------------------------------------------------

		plot.setRangeAxis(1, yAxis1);

		plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);

		plot.setDataset(1, createDataset1());

		plot.mapDatasetToRangeAxis(1, 1);

		XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();

		renderer2.setSeriesLinesVisible(0, true);

		renderer2.setSeriesLinesVisible(1, true);

		renderer2.setSeriesLinesVisible(2, true);

		
		renderer2.setSeriesShapesVisible(0, true);
		

		renderer2.setSeriesShapesVisible(1, true);

		renderer2.setSeriesShapesVisible(2, true);

		renderer2.setSeriesPaint(0, new java.awt.Color(18, 193, 118));

		renderer2.setSeriesPaint(1, new java.awt.Color(204, 51, 51));

		renderer2.setSeriesPaint(2, new java.awt.Color(51, 102, 255));
		
		

		renderer2.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition(

		ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT));

		renderer2.setSeriesPositiveItemLabelPosition(1, new ItemLabelPosition(

		ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT));

		renderer2.setSeriesPositiveItemLabelPosition(2, new ItemLabelPosition(

		ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT));

		renderer2.setItemLabelFont(new Font("Arial", Font.BOLD, 11));
		// renderer2.setBase
		renderer2.setBaseItemLabelsVisible(true);
		renderer2.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot
		.getLegendItems()));
		//Shape shape2  =ShapeUtilities.createUpTriangle(1);
		//renderer2.setBaseShape(shape2);
		//renderer2.setBasePaint(Color.red);
		//renderer.setSeriesShape(0, shape2);
		plot.setRenderer(1, renderer2);

		// ----------------------------------------------------

		JFreeChart chart = new JFreeChart(

		"Accumulated benifit left on deposit",

		JFreeChart.DEFAULT_TITLE_FONT, plot, false);

		JFrame frame = new JFrame("LogAxis Test");

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setContentPane(new ChartPanel(chart));

		frame.pack();

		frame.setVisible(true);

	}

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {

				final BruceWayneChart demo = new BruceWayneChart(

				"Accumulated Benifit left on deposit");

				demo.createFrame();

			}

		});

	}

}
